#!/bin/bash

# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2009-2016 Stephan Raue (stephan@openelec.tv)
# Copyright (C) 2016-2018 Team LibreELEC (https://libreelec.tv)
# Copyright (C) 2018-present Team CoreELEC (https://coreelec.org)
# Copyright (C) 2023 JELOS (https://github.com/JustEnoughLinuxOS)

unset _CACHE_PACKAGE_LOCAL _CACHE_PACKAGE_GLOBAL _DEBUG_DEPENDS_LIST _DEBUG_PACKAGE_LIST

. config/options ""
. config/multithread
. config/show_config

show_config

${SCRIPTS}/checkdeps

# Setup both toolchain cmake configs to avoid potentially racy behaviour later.
# Use a fork for host to isolate any variable modifications.
( setup_toolchain host )
setup_toolchain target

function do_mkimage() {
  # Variables used in mkimage script must be passed
  env \
    ROOT="${ROOT}" \
    SCRIPTS="${SCRIPTS}" \
    TOOLCHAIN="${TOOLCHAIN}" \
    PROJECT_DIR="${PROJECT_DIR}" \
    PROJECT="${PROJECT}" \
    DEVICE="${DEVICE}" \
    DISTRO="${DISTRO}" \
    TARGET_IMG="${TARGET_IMG}" \
    BUILD_NAME="${IMAGE_NAME}" \
    IMAGE_NAME="${1:-${IMAGE_NAME}}" \
    BOOTLOADER="${BOOTLOADER}" \
    KERNEL_NAME="${KERNEL_NAME}" \
    TARGET_KERNEL_ARCH="${TARGET_KERNEL_ARCH}" \
    RELEASE_DIR="${RELEASE_DIR}" \
    UUID_SYSTEM="${UUID_SYSTEM}" \
    UUID_STORAGE="${UUID_STORAGE}" \
    DISTRO_BOOTLABEL="${DISTRO_BOOTLABEL}" \
    DISTRO_DISKLABEL="${DISTRO_DISKLABEL}" \
    UBOOT_SYSTEM="${UBOOT_SYSTEM}" \
    UBOOT_VERSION="${UBOOT_VERSION}" \
    EXTRA_CMDLINE="${EXTRA_CMDLINE}" \
    SYSTEM_SIZE="${SYSTEM_SIZE}" \
    SYSTEM_PART_START="${SYSTEM_PART_START}" \
    SUBDEVICE="${SUBDEVICE}" \
    ${SCRIPTS}/mkimage
}

if [ -n "${CUSTOM_GIT_HASH}" ]; then
  GIT_HASH="${CUSTOM_GIT_HASH}"
else
  GIT_HASH=$(git rev-parse HEAD)
fi

# Get origin url, fix git:// and git@github.com: urls if necessary
ORIGIN_URL="$(git remote -v | awk '$1 == "origin" { print $2 }' | head -1 | sed 's#\.git$##;s#^git:#https:#;s#^git@github\.com:#https://github.com/#')"

if [ -n "${CUSTOM_VERSION}" ]; then
  export OS_VERSION="${CUSTOM_VERSION}"
fi

OS_ARCH="${DEVICE:-${PROJECT}}.${TARGET_ARCH}"
TARGET_VERSION="${OS_ARCH}-${OS_VERSION}"

if [ -n "${CUSTOM_IMAGE_NAME}" ]; then
  IMAGE_NAME="${CUSTOM_IMAGE_NAME}"
else
  IMAGE_NAME="${DISTRONAME}-${TARGET_VERSION}"
fi

if [ -n "${IMAGE_SUFFIX}" ]; then
  IMAGE_NAME="${IMAGE_NAME}-${IMAGE_SUFFIX}"
fi

echo "${IMAGE_NAME}" > ${BUILD}/BUILD_FILENAME

# Setup fakeroot
rm -rf ${FAKEROOT_SCRIPT}   # remove ${FAKEROOT_SCRIPT} if it exist
touch ${FAKEROOT_SCRIPT}    # create an empty ${FAKEROOT_SCRIPT}
chmod +x ${FAKEROOT_SCRIPT} # make ${FAKEROOT_SCRIPT} executable
echo "chown -R 0:0 ${INSTALL}" >> ${FAKEROOT_SCRIPT}

# Clean old install dirs
rm -rf ${INSTALL}
rm -rf ${STAMPS_INSTALL}
mkdir -p ${INSTALL}

# Create base layout of JELOS read-only file system
for directory in etc dev proc run sys tmp usr var flash storage; do
  mkdir -p ${INSTALL}/${directory}
done

MTADDONBUILD=no start_multithread_build ${1} || die "Parallel build failure. Time of failure: $(date)"
echo "Successful build!  Building compatibility root..." >&2
exit 0

